<template>
	<view class="bgs">
		<view class="ltitle fs32 bold mb30">
			直播间弹幕列表
		</view>
		<scroll-view id="listBox" ref="scroll" scroll-y="true" :show-scrollbar="true" :scroll-top="scrollTop" class="scr">
			<view class="listBox" v-show="list.length">
				<view class="litem df message" :id="'msg'+idx" v-for="(i,idx) in list" :key="idx" ref="list">
					<view class="main">
						{{i.name}}: {{i.content}}
					</view>
				</view>
			</view>
			<view class="pt30" v-show="!list.length">
				<image src="../../static/img/noMsg.png" width="140rpx" height="160rpx" mode="aspectFit"></image>
				<view class="text-center" style="color: #97999f;">
					暂无弹幕
				</view>
			</view>
		</scroll-view>

	</view>
</template>
<script>
	export default {
		name: "msgList",
		props: {
			msgArr: {
				default: () => ([]),
				type: Array,
			},
		},
		data() {
			return {
				scrollTop: 0,
				list: [],
			};
		},
		watch: {
			msgArr: {
				handler(val) {
					this.scrollTop = 0
					console.log(val);
					if (val.length) this.list= val;
					if (this.list.length >= 100) {
						this.list.splice(0, 50)
					}
					this.$nextTick(() => {
						const query = uni.createSelectorQuery().in(this);
						query.select('#listBox').scrollOffset(data => {
							this.scrollTop = data.scrollHeight
						}).exec();
					})
				},
				immediate: true,
				deep: true,
			},
		},
		methods: {}
	}
</script>

<style lang="less">
	.bgs {
		background: #fff;
		border-radius: 16rpx;
		color: #000;
		margin: 40rpx 30rpx;
		padding: 30rpx;
	}

	.listBox {
		background: #f1f3f4;
		border-radius: 16rpx;
		color: #000;
		height: 55vh;
		// overflow: scroll;

		.litem {
			// margin-bottom: 16rpx;
			background: #fff;
			// border-radius: 16rpx;
			padding: 20rpx;
			flex-wrap: wrap;
			border-bottom: 2px solid #f5f5f5;

			.main {
				width: 80%;
			}

			.replay {
				width: 10%;
				font-size: 26rpx;
				border-radius: 16rpx;
				border: 1px solid #308ffc;
				padding: 0rpx 10rpx;
				text-align: center;
				height: 45rpx;
			}
		}
	}
</style>